/* $RCSFile: ADNDynAppointment.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynAppointment.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynAppointment : ADNDynActivity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.appointment)
            {}

            public override Type DynType
            { get { return typeof(ADNDynAppointment); } }

            public override string DynTypename
            {   get{ return CrmStr.appointment;}}

            public override DynEntity Build()
            {   return new ADNDynAppointment();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynAppointment(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynAppointment(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynAppointment(w,PrimaryKeyValue);}
        }

        static ADNDynAppointment(){new Ftor();} 

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynAppointment(): base()
		{}

        public ADNDynAppointment(CRMWrapperBase w,string subj)
			: base(w,subj,CrmStr.appointment)
		{}

        public ADNDynAppointment(CRMWrapperBase w,string subj,string body,DynEntity sender,DynEntity dest)
			: base(w, subj,CrmStr.appointment)
		{
            description =   body;
            AddOrganizer(sender);
            AddRequiredAttende(dest);
		}


        public ADNDynAppointment(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.appointment, CrmId)
		{}

		public ADNDynAppointment(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.appointment,e,null,null)
		{}

		public ADNDynAppointment(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.appointment,e,prop,s)
		{}

        public ADNDynAppointment(ResetWrapperHandler fn,string subj)
			: base(fn,subj,CrmStr.appointment)
		{}

        public ADNDynAppointment(ResetWrapperHandler fn,string subj,string body,DynEntity sender,DynEntity dest)
			: base(fn, subj,CrmStr.appointment)
		{
            description =   body;
            AddOrganizer(sender);
            AddRequiredAttende(dest);
		}

        public ADNDynAppointment(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.appointment, CrmId)
		{}

		public ADNDynAppointment(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.appointment,e,null,null)
		{}

		public ADNDynAppointment(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.appointment,e,prop,s)
		{}

        /// <summary>
        ///  Contains the information on whether the appointment is an all-day event. 
        /// </summary>
        [CrmDynAttribute(CrmStr.appointment)]
        public bool isalldayevent
        {
            get
            {
                return BooleanPropertyValue(CrmStr.isalldayevent);
            }
            set
            {
                AddBooleanProperty(CrmStr.isalldayevent,value);
            }
        }

        /// <summary>
        ///  Gets or sets the location where the appointment is to occur. 
        /// </summary>
        [CrmDynAttribute(CrmStr.appointment)]
        public string location
        {
            get
            {
                return StringPropertyValue(CrmStr.location);
            }
            set
            {
                AddStringProperty(CrmStr.location,value);
            }
        }
 
        /// <summary>
        /// Gets or sets the ID of the e-mail message. Used only for e-mail that is received. 
        /// </summary>
        [CrmDynAttribute(CrmStr.appointment)]
        public string messageid
        {
            get
            {
                return StringPropertyValue(CrmStr.messageid);
            }
            set
            {
                AddStringProperty(CrmStr.messageid,value);
            }
        }
 
        /// <summary>
        ///  Gets or sets the unique Microsoft Office Outlook ID to correlate appointments across Exchange Server mailboxes. 
        /// </summary>
        [CrmDynAttribute(CrmStr.appointment)]
        public string globalobjectid
        {
            get
            {
                return StringPropertyValue(CrmStr.globalobjectid);
            }
            set
            {
                AddStringProperty(CrmStr.globalobjectid,value);
            }
        }

        /// <summary>
        /// Gets or sets the Microsoft Office Outlook ID for the appointment owner that correlates to the PR_OWNER_APPT_ID MAPI property. 
        /// </summary>
        [CrmDynAttribute(CrmStr.appointment)]
        public int outlookownerapptid
        {
            get
            {
                return NumberPropertyValue(CrmStr.outlookownerapptid);
            }
            set
            {
                AddNumberProperty(CrmStr.outlookownerapptid,value);
            }
        }
        
        /*
        bcc Gets or sets the blind carbon copy (BCC) recipients. 
        cc Gets or sets the carbon copy (CC) recipients. 
        from Gets or sets the information about who sent the e-mail. 
        to Gets or sets the recipient party list for the e-mail, and references recipient records such as users and queues. 
        */
        public List<ADNDynActivityParty> organizerList           =   new List<ADNDynActivityParty>();
        public List<ADNDynActivityParty> optionalattendeesList   =   new List<ADNDynActivityParty>();
        public List<ADNDynActivityParty> requiredattendeesList   =   new List<ADNDynActivityParty>();

        public void AddOrganizer(DynEntity ent)
        {
            ADNDynActivityParty ap   =   new ADNDynActivityParty(wrapper);
            ap.DefaultPartyName     =   ent.SchemaName;
            ap.partyid              =   ent.PrimaryKeyGuidValue;
            organizerList.Add(ap);
        }

        public void AddRequiredAttende(DynEntity ent)
        {
            ADNDynActivityParty ap   =   new ADNDynActivityParty(wrapper);
            ap.DefaultPartyName     =   ent.SchemaName;
            ap.partyid              =   ent.PrimaryKeyGuidValue;
            requiredattendeesList.Add(ap);
        }

        public void AddOptionalAttende(DynEntity ent)
        {
            ADNDynActivityParty ap   =   new ADNDynActivityParty(wrapper);
            ap.DefaultPartyName     =   ent.SchemaName;
            ap.partyid              =   ent.PrimaryKeyGuidValue;
            optionalattendeesList.Add(ap);
        }

        [CrmDynAttribute(CrmStr.appointment)]
        public InternalDynamicEntityArray organizer
        {
            get
            {
                return DynamicEntityArrayProperty(CrmStr.organizer);
            }
            set
            {
                AddDynamicEntityArrayProperty(CrmStr.organizer,value);
            }
        }

        [CrmDynAttribute(CrmStr.appointment)]
        public InternalDynamicEntityArray requiredattendees
        {
            get
            {
                return DynamicEntityArrayProperty(CrmStr.requiredattendees);
            }
            set
            {
                AddDynamicEntityArrayProperty(CrmStr.requiredattendees,value);
            }
        }

        [CrmDynAttribute(CrmStr.appointment)]
        public InternalDynamicEntityArray optionalattendees
        {
            get
            {
                return DynamicEntityArrayProperty(CrmStr.optionalattendees);
            }
            set
            {
                AddDynamicEntityArrayProperty(CrmStr.optionalattendees,value);
            }
        }

        protected override Guid internalCreate(bool isAdmin)
        {
            if ( (organizerList.Count == 0) && !dicProperty.ContainsKey(CrmStr.organizer) )
            {
                const string msg    =   "[E]ADNDynAppointment internalCreate 'organizer' list empty";
                if ( CRMDynTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( msg,CRMDynTraceSwitch.Sw.Info);
                //throw new Exception(msg);
            }
            if ( (requiredattendeesList.Count == 0) && !dicProperty.ContainsKey(CrmStr.requiredattendees) )
            {
                const string msg    =   "[E]ADNDynAppointment internalCreate 'requiredattendees' list empty";
                if ( CRMDynTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( msg,CRMDynTraceSwitch.Sw.Info);
                //throw new Exception(msg);
            }
            if (organizerList.Count > 0)
            {
                InternalDynamicEntityArray newArray = wrapper.Util.MakeDynamicArray(organizerList);
                AddDynamicEntityArrayProperty(CrmStr.organizer, newArray);
            }

            if (requiredattendeesList.Count > 0)
            {
                InternalDynamicEntityArray newArray = wrapper.Util.MakeDynamicArray(requiredattendeesList);
                AddDynamicEntityArrayProperty(CrmStr.requiredattendees, newArray);
            }

            if (optionalattendeesList.Count > 0)
            {
                InternalDynamicEntityArray newArray = wrapper.Util.MakeDynamicArray(optionalattendeesList);
                AddDynamicEntityArrayProperty(CrmStr.optionalattendees, newArray);
            }

            return wrapper.CreateFromDynEntity(this,isAdmin);
        }
    }
}
